package org.jeecg.modules.equipment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.exception.IllegalEquipmentException;
import org.jeecg.modules.equipment.controller.EquipmentQuery;
import org.jeecg.modules.equipment.entity.Equipment;
import org.jeecg.modules.equipment.mapper.EquipmentMapper;
import org.jeecg.modules.equipment.service.IEquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: 设备
 * @Author: jeecg-boot
 * @Date:   2019-10-03
 * @Version: V1.0
 */
@Service
public class EquipmentServiceImpl extends ServiceImpl<EquipmentMapper, Equipment> implements IEquipmentService {


    @Autowired
    private EquipmentMapper equipmentMapper;

    @Override
    public IPage<Equipment> page(IPage<Equipment> page, EquipmentQuery equipmentQuery) {
        List<Equipment> list = equipmentMapper.page(page, equipmentQuery);
        page.setRecords(list);
        return page;
    }

    @Override
    public List<Equipment> findByAdminUserId(String adminUserId) {
        Equipment equipment = new Equipment();
        equipment.setAdminUserId(adminUserId);
        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("admin_user_id",adminUserId);
        List<Equipment> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public Equipment findByCode(String code) {
        List<Equipment> list = equipmentMapper.findByCode(code);
        if (list.size() > 0) {
            return list.get(0);
        } else {
            throw new IllegalEquipmentException("Invalid device!");
        }
    }
}
